package JDBC.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Insert {
    public static void main(String[] args) throws Exception {
//        注册驱动（弊端：重复执行）
        DriverManager.registerDriver(new Driver());
//        建立连接
        Connection connection = DriverManager.getConnection
                ("jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456");

        //        编写sql即添加操作(在java中编写sql，结尾  可以省略)
        String sql = "insert into account(id,name,money) values(?,?,?)";

        //    获取sql执行对象
//        Statement statement = connection.createStatement();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
//        设置问号的参数----1,2,3分别对应 id,name,money
   /*     preparedStatement.setInt(1,7);
        preparedStatement.setString(2,"测试三");
        preparedStatement.setInt(3,10000);
*/
        //        执行sql并返回结果
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        System.out.println(i);

        //        释放资源
        preparedStatement.close();
        connection.close();
    }

}
